package bank
{
	import events.TabEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldType;
	
	import utils.DisplayTool;
	import utils.StringUtils;
	import utils.OpenWindow;
	
	import global.GameState;
	import view.LabelText;
	import view.components.IconButton;
	import view.components.Input;
	import view.components.LabelButton;
	import view.components.list.TileList;
	import view.components.tab.Tab;
	import view.components.tab.TabBar;
	
	public class BankLeft extends Sprite
	{
		private var bankLandControl:BankLandControl = BankLandControl.getInstance();
		private var tab:Tab;
		private var friendList:TileList;
		private var levelList:TileList;
		internal var prevBut:LabelButton;
		internal var nextBut:LabelButton;
		private var title:LabelText;
		internal var searchInput:Input;
		
		private var refreshBut:IconButton;
		private var searchResult:SearchResult;
		private var inviteBut:IconButton;
		
		public function BankLeft()
		{
			this.mouseEnabled = false;
			initTab();
			initList();
			initLeftRightBut();
			initTitle();
			initSearchInput();
			initIconBut();
			initSeachResult();
		}
		
		private function initSeachResult():void
		{
			searchResult = new SearchResult();
			
			searchResult.y = 40;
			//
		}
		
		private function initIconBut():void
		{
			refreshBut = new IconButton(76,325,0,onRefresh,"panel_refresh","刷新列表");
			refreshBut.mouseEnabled = false;
			addChild(refreshBut);
			
			inviteBut = new IconButton(2,325,0,onInvite,"panel_plus","邀请好友");
			addChild(inviteBut);
		}
		
		private function onInvite(event:MouseEvent):void
		{
			OpenWindow.openWindow(GameState.getInviteLink());
		}
		
		private function onRefresh(event:MouseEvent):void
		{
			bankLandControl.refreshList(tab.selectedIndex);
			if (friendList.visible) friendList.removeAll();
			if (levelList.visible) levelList.removeAll();
			refreshBut.mouseEnabled = false;
		}
		
		private function initSearchInput():void
		{
			searchInput = new Input(100,20);
			searchInput.type = TextFieldType.INPUT;
			searchInput.textField.maxChars = 100;
			searchInput.x = 26;
			searchInput.y = 17;
			
			searchInput.text = "您要找...";
			
			addChild(searchInput);
			
			searchInput.textField.addEventListener(Event.CHANGE,onSearchChange);
		}
		
		private function onSearchChange(event:Event):void
		{
			var key:String = StringUtils.trim(searchInput.text);
			
			if (key != "")
			{
				addChild(searchResult);
				searchResult.seach(key);
			}
			else
			{
				DisplayTool.removeChild(searchResult.parent,searchResult);
			}
		}
		
		private function initTitle():void
		{
			title = new LabelText(37,-13,100,20,"好友的银行",0xFFFFFF,14,true);
			addChild(title);
		}
		
		private function initList():void
		{
			friendList = new TileList(150,240,new ItemBankFriend());
			friendList.y = 80;
			friendList.drawBg();
			addChild(friendList);
			friendList.visible = false;
			
			levelList = new TileList(150,240,new ItemBank());
			levelList.y = 80;
			levelList.drawBg();
			addChild(levelList);
		}
		
		private function initTab():void
		{
			tab = new Tab();
			tab.addTabBar(new TabBar(0,"可取",65),true);
			tab.addTabBar(new TabBar(0,"好友",65));
			tab.x = 10;
			tab.y = 50;
			addChild(tab);
			
			tab.addEventListener(TabEvent.TABBAR_CLICK,onTab);
		}
		
		private function onTab(event:TabEvent):void
		{
			if (event.index == 0)	
			{
				levelList.visible = true;
				friendList.visible = false;
				friendList.clearSelectedItem();
			}
			else
			{
				levelList.visible = false;
				friendList.visible = true;
				levelList.clearSelectedItem();
			}
			
			
		}
		
		
		
		private function initLeftRightBut():void
		{
			prevBut = new LabelButton(0,15,0,onBut,"panel_left2","上一个好友");
			nextBut = new LabelButton(this.width - 23,15,1,onBut,"panel_right2","下一个好友");
			addChild(prevBut);
			addChild(nextBut);
			
			prevBut.mouseEnabled = false;
			nextBut.mouseEnabled = false;
		}
		
		private function onBut(event:MouseEvent):void
		{
			prevBut.mouseEnabled = false;
			nextBut.mouseEnabled = false;
			bankLandControl.gotFriend(event.target.id);
			
			friendList.clearSelectedItem();
			levelList.clearSelectedItem();	
		}
		
		public function updateLevel(xml:XML):void
		{
			levelList.removeAll();
			var data:XML;
			for each (data in xml.children())
			{
				levelList.addItem(data);
			}
			
			if (levelList.visible)
			{
				refreshBut.mouseEnabled = true;
			}
		}
		
		
		private var friendXml:XML;
		public function updateFriend(xml:XML):void
		{
			friendXml = xml;
			friendList.removeAll();
			var data:XML;
			for each (data in xml.children())
			{
				friendList.addItem(data);
			}
			
			if (friendList.visible)
			{
				refreshBut.mouseEnabled = true;
			}
		}
	}
}